package com.qingke.Command;

import java.util.HashMap;
import java.util.Map;

import com.qingke.business.CreateCommand;
import com.qingke.business.DeleteCommand;
import com.qingke.business.ExitCommand;
import com.qingke.business.HelpCommand;
import com.qingke.business.InquireCommand;
import com.qingke.business.ListCommand;
import com.qingke.business.UpdateCommand;

public class UserCommandFactory  extends CommandFactory{
	
	
	private static Map<CommandCode,Class<?extends Command>> commandMap = new HashMap<>();
	
	static {
		commandMap.put(CommandCode.DELETE, DeleteCommand.class);
		commandMap.put(CommandCode.LIST, ListCommand.class);
		commandMap.put(CommandCode.UPDATE, UpdateCommand.class);
		commandMap.put(CommandCode.EXIT, ExitCommand.class);
		commandMap.put(CommandCode.HELP, HelpCommand.class);
		commandMap.put(CommandCode.INQUIRE, InquireCommand.class);
		commandMap.put(CommandCode.CREATE, CreateCommand.class);
	}

	public Command buildCommand(CommandCode cmdCode) {
		Command command = null;
		Class<? extends Command> cmdClass = commandMap.get(cmdCode);
		if(cmdClass != null){
			try {
				command = cmdClass.newInstance();
			} catch (InstantiationException | IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return command;
	}
	
}
